﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Timetable.Base.Entities;
using Timetable.Base.Interfaces.Services.DataServices.Extensions;

namespace Timetable.Site.Controllers.Api
{
    public class LecturerController : BaseApiController<Lecturer>
    {
        /// <summary>
        /// Возврашает список преподавателей подпадающих под фильтр
        /// </summary>
        /// <param name="value">Фильтр</param>
        /// <returns>Список преподавателей</returns>
        public HttpResponseMessage Get(string value)
        {
            return CreateResponse<string, IEnumerable<string>>(privateGet, value);
        }
        private IEnumerable<string> privateGet(string value)
        {
            var lecturers = DataService.GetsByFirstMiddleLastname(value);

            var result = lecturers.Select(x => x.Lastname + " " + x.Firstname + " " + x.Middlename);

            return result.AsEnumerable();
        }

        /// <summary>
        /// Return lecturers for current tutorial
        /// </summary>
        /// <param name="tutorialId"></param>
        /// <returns></returns>
        public HttpResponseMessage GetByTutorialId(int tutorialId)
        {
            return CreateResponse<int, IEnumerable<string>>(privateGetByTutorialId, tutorialId);
        }
        private IEnumerable<string> privateGetByTutorialId(int tutorialId)
        {
            var lecturers = DataService.GetByTutorialId(tutorialId);

            var result = lecturers.Select(x => String.Format("{0} {1}. {2}.", x.Lastname, x.Firstname[0], x.Middlename[0]));

            return result.AsEnumerable();
        }

        public HttpResponseMessage GetByDepartment(int departmentId)
        {
            return CreateResponse<int, IEnumerable<Lecturer>>(privateGetByDepartment, departmentId);
        }
        private IEnumerable<Lecturer> privateGetByDepartment(int departmentId)
        {
            return DataService.GetByDepartmentId(departmentId).OrderBy(x => x.Middlename).OrderBy(x => x.Firstname).OrderBy(x => x.Lastname);
        }
    }
}